<?php


namespace app\admin\member\controller;


use app\admin\member\validate\MemberLevelValidate;
use app\common\controller\BaseController;
use app\model\MemberLevel;

class MemberLevelController extends BaseController
{
    protected $model = MemberLevel::class;

    public function list()
    {
        $form = request()->getForm();

        $where = [];
        $form['name'] && $where[] = ['name', 'like', "%" . $form['name'] . "%"];
        $count = $this->getModel()
            ->where($where)
            ->count();

        $data = $this->getModel()
            ->where($where)
            ->limit($form->getOffset(), $form->getPageSize())
            ->select();
        return $this->success($this->formatPage($count, $data));
    }

    public function add()
    {
        $data = request()->all();
        MemberLevelValidate::goCheck($data, 'add');
        $this->getModel()->insert($data);
        return $this->success();
    }

    public function update()
    {
        $data = request()->all();
        MemberLevelValidate::goCheck($data, 'update');
        $this->getModel()->where('id', '=', $data['id'])->update($data);
        return $this->success();
    }

    public function getById()
    {
        $id = input('id');
        MemberLevelValidate::goCheck(input(), 'id');
        $data = $this->getModel()->where('id', '=', $id)->find();
        return $this->success($data);
    }

    public function deleteById()
    {
        $id = input('id');
        MemberLevelValidate::goCheck(input(), 'id');
        $this->getModel()->where('id', '=', $id)->delete();
        return $this->success();
    }

}